package com.hongye.app.model;

import java.util.List;

import com.hongye.util.Utils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

public class BuyerStatisticsModel extends Model<BuyerStatisticsModel>{
	
	private static final long serialVersionUID = -3178808507706403714L;
	public static final BuyerStatisticsModel dao = new BuyerStatisticsModel();
	
	/**
	 * 通过电话获取用户统计
	 * @author lifuqiang
	 * 2016-12-16
	 * @param phone
	 * @return
	 */
	public static BuyerStatisticsModel getStatistics(String phone){
		String sql = "select * from buyer_statistics where buyer_phone = '"+phone+"'";
		return BuyerStatisticsModel.dao.findFirst(sql);
	}
	
	/**
	 * 通过id获取用户统计
	 * @author liushaomin
	 * 2017年3月17日
	 * @param userid
	 * @return
	 */
	public static BuyerStatisticsModel getStatisticsByID(String userid){
		String sql = "select * from buyer_statistics where buyer_id = '"+ userid +"'";
		return BuyerStatisticsModel.dao.findFirst(sql);
	}
	
	 /**
	 * 根据用户id 获取用户统计
	 * @author liushaomin
	 * 2016年12月16日
	 * @param buyer_id
	 * @return
	 */
	public static BuyerStatisticsModel findByUser_id(String buyer_id) {
		String sql="select * from buyer_statistics where buyer_id ='"+buyer_id+"'";
		BuyerStatisticsModel user = dao.findFirst(sql);
		return user;
	}
	
	/**
	 * 开通店铺用户数量
	 * @author lifuqiang
	 * 2017-4-27
	 */
	public static long OpenShopNumber(){
		String sql="select * from buyer_statistics where isshop in(1,2,3)";
		List<BuyerStatisticsModel> list = dao.find(sql);
		return list.size();
	}
	
	/**
	 * 买家评论后  更新商家信誉度
	 * 商品打分权重：0.5，服务打分权重为：0.25，物流打分权重为：0.25，信誉度=（描述相符评分+服务态度评分+物流服务评分）根据权重求和/所有评论人
	 * @author liushaomin
	 * 2016年12月16日
	 * @param seller_id 商家id
	 * @param servicelevel 服务打分
	 * @param logisticlevel 物流打分
	 * @param goodslevel 商品打分
	 */
	public static boolean UpdateSeller_credit(String seller_id) {
		BuyerStatisticsModel user = findByUser_id(seller_id);
		List<EvaluationModel> models = EvaluationModel.getEvaluBySeller_id(seller_id);
		double sum = 0;
		for (EvaluationModel item : models) {
			double itemsum = Utils.add(Utils.add(Utils.muti(item.getInt("service_level"), 0.25), Utils.muti(item.getInt("logistic_level"), 0.25)), Utils.muti(item.getInt("goods_level"), 0.5));
			sum = Utils.add(sum, itemsum);
		}
		return user.set("seller_credit", Utils.div(sum, models.size())).update();
	}
	
	/**
	 * 获取店铺名称
	 * @author liushaomin
	 * 2017年3月10日
	 * @param buyer_id
	 * @return
	 */
	public static String getShop_name(String buyer_id) {
		String sql="select shop_name from buyer_statistics where buyer_id ='"+buyer_id+"'";
		BuyerStatisticsModel user = dao.findFirst(sql);
		return user.getStr("shop_name");
	}
	
	/**
	 * 获取店铺名称
	 * @author liushaomin
	 * 2017年3月10日
	 * @param buyer_id
	 * @return
	 */
	public static String getShop_nameByPhone(String phone) {
		String sql="select shop_name from buyer_statistics where buyer_phone ='"+phone+"'";
		BuyerStatisticsModel user = dao.findFirst(sql);
		return user.getStr("shop_name");
	}
	
	/**
	 * 根据店铺名称 查询店铺
	 * @author liushaomin
	 * 2017年4月5日
	 * @param shopname
	 * @return
	 */
	public static List<BuyerStatisticsModel> getShopByName(String shopname) {
		String sql="select * from buyer_statistics where shop_name ='"+shopname+"' and isshop = 1";
		List<BuyerStatisticsModel> shop = dao.find(sql);
		return shop;
	}
	
	/**
	 * 获取店铺头像 by phone
	 * @author liushaomin
	 * 2017年3月10日
	 * @param buyer_id
	 * @return
	 */
	public static String getShop_avatarByPhone(String phone) {
		String sql="select shop_avatar from buyer_statistics where buyer_phone ='"+phone+"'";
		BuyerStatisticsModel user = dao.findFirst(sql);
		return user.getStr("shop_avatar");
	}
	
	/**
	 * 获取店铺所在城市（商品发货地）
	 * @author liushaomin
	 * 2017年3月10日
	 * @param buyer_id
	 * @return
	 */
	public static String getAddress(String buyer_id) {
		String sql="select shop_address from buyer_statistics where buyer_id ='"+buyer_id+"'";
		BuyerStatisticsModel user = dao.findFirst(sql);
		return user.getStr("shop_address");
	}
	
	/**
	 * 通过邀请码（员工编号）获取所有店铺数量
	 * @author lifuqiang
	 * 2017-3-20
	 * @param employee_num
	 * @return
	 */
	public static long getInviteNum(String employee_num){
		return Db.queryLong("select count(1) from buyer_statistics where employee_invite_code  = '" +employee_num+ "'");
	}
	
	/**
	 * 通过邀请码（员工编号）获取所有店铺
	 * @author lifuqiang
	 * 2017-3-20
	 * @param employee_num
	 * @return
	 */
	public static List<BuyerStatisticsModel> getBS_By_EmployeeNum(String employee_num){
		String sql = "select * from buyer_statistics where employee_invite_code  = '" +employee_num+ "'";
		List<BuyerStatisticsModel> list = dao.find(sql);
		return list;
	}
	
	/**
	 * 获取员工邀请的店铺列表(时间筛选)
	 * @author lifuqiang
	 * 2017-3-21
	 * @param time_start
	 * @param time_end
	 * @param  employee_num
	 * @return
	 */
	public static List<BuyerStatisticsModel> getBS_ByScreen(String time_start, String time_end ,String  employee_num){
		String sql="select * from buyer_statistics where employee_invite_code = '"+ employee_num+"' and  shop_c_time >= '"+time_start+"' and shop_c_time <= '"+time_end+"' order by shop_c_time desc";
		return dao.find(sql);
	}

	/**
	 * 增加商家可提现账户的金额 + 可提现金额
	 * 增加可提现  减少不可提现 （买家确认收货后、自动确认收货后的操作）
	 * @author liushaomin
	 * 2017年4月13日
	 * @param orderid
	 */
	public static void Update_Add_Usable(OrderInfoModel order) {
		List<OrderGoodsModel> order_goods = OrderGoodsModel.getByOrderId(order.getInt("id"));
		BuyerStatisticsModel seller_buyer = getStatisticsByID(order_goods.get(0).getInt("seller_id").toString());
		double shop_usable_account = Utils.add(seller_buyer.getDouble("shop_usable_account"), order.getFloat("total_price"));
		double shop_disable_account = 0.0;
		if(seller_buyer.getDouble("shop_disable_account") > 0){
			shop_disable_account = Utils.subtract(seller_buyer.getDouble("shop_disable_account"), order.getFloat("total_price"));
		}
		boolean boo = seller_buyer.set("shop_usable_account", shop_usable_account).set("shop_disable_account", shop_disable_account).update();
		if(boo){
			TakeOutModel takeout = new TakeOutModel();
			takeout.set("buyer_id", seller_buyer.getInt("buyer_id")).set("buyer_phone", seller_buyer.get("buyer_phone")).set("money", order.getFloat("total_price"))
				   .set("leave_money", shop_usable_account).set("content", "卖出商品").set("status", 1).set("type", 0).set("c_time", Utils.getDate()).save();
		}
	}
	
	/**
	 * 减少商家可提现账户的金额 
	 * 申请提现时操作
	 * @author lifuqiang
	 * 2017-4-19
	 * @param phone
	 * @param money
	 * @return
	 */
	public static boolean Update_Reduce_Usable(String phone, double money){
		BuyerStatisticsModel bs = getStatistics(phone);
		double shop_disable_account = Utils.subtract(bs.getDouble("shop_usable_account"), money);
		boolean boo = bs.set("shop_usable_account", shop_disable_account).update();
		return boo;
	}
	
	/**
	 * 增加商家不可提现账户的金额 + 不可提现金额
	 * 可提现金额不变 增加不可提现金额 (买家支付后)
	 * @author liushaomin
	 * 2017年4月13日
	 * @param orderid
	 */
	public static void Update_Add_Disable(OrderInfoModel order) {
		List<OrderGoodsModel> order_goods = OrderGoodsModel.getByOrderId(order.getInt("id"));
		BuyerStatisticsModel seller_buyer = getStatisticsByID(order_goods.get(0).getInt("seller_id").toString());
		double shop_disable_account = Utils.add(seller_buyer.getDouble("shop_disable_account"), order.getFloat("total_price"));
		seller_buyer.set("shop_disable_account", shop_disable_account).update();
		
	}
	
	/**
	 * 减少商家不可提现账户的金额 - 不可提现
	 * 可提现金额不变 减少不可提现的金额（商家同意退款，自动退款，退货等）
	 * @author liushaomin
	 * 2017年4月13日
	 * @param order
	 */
	public static void Update_Reduce_Disable(OrderInfoModel order) {
		List<OrderGoodsModel> order_goods = OrderGoodsModel.getByOrderId(order.getInt("id"));
		BuyerStatisticsModel seller_buyer = getStatisticsByID(order_goods.get(0).getInt("seller_id").toString());
		double shop_disable_account = 0.0;
		if(seller_buyer.getDouble("shop_disable_account") > 0){
			shop_disable_account = Utils.subtract(seller_buyer.getDouble("shop_disable_account"), order.getFloat("total_price"));
		}
		seller_buyer.set("shop_disable_account", shop_disable_account).update();
	}
	
	/**
	 * 获取成交总额（钱包）
	 * @author liushaomin
	 * 2017年4月14日
	 * @param useruid
	 * @return
	 */
	public static double getTotal_money(String phone) {
		BuyerStatisticsModel buyer = getStatistics(phone);
		double total_mone = Utils.add(buyer.getDouble("shop_usable_account"), buyer.getDouble("shop_disable_account"));
		return total_mone;
	}
	
	/**
	 * 得到某人钱包页面的信息
	 * @author liushaomin
	 * 2017年4月19日
	 * @param phone
	 * @return
	 */
	public static BuyerStatisticsModel getBuyerWallet(String phone) {
		String sql = "select shop_usable_account, shop_disable_account, buyer_phone, buyer_id from buyer_statistics where buyer_phone = '"+ phone +"'";
		BuyerStatisticsModel buyer = dao.findFirst(sql);
		buyer.put("total_money", Utils.add(buyer.getDouble("shop_usable_account"), buyer.getDouble("shop_disable_account")));
		return buyer;
	}
	
	/**
	 * 准备申请提现
	 * @author lifuqiang
	 * 2017-4-19
	 * @param phone
	 * @return
	 */
	public static BuyerStatisticsModel planApplyTakeout(String phone) {
		String sql = "select shop_usable_account, buyer_phone, shop_bankcard, shop_banktype from buyer_statistics where buyer_phone = '"+ phone +"'";
		BuyerStatisticsModel bs = dao.findFirst(sql);
		return bs;
	}
	
	/**
	 * 得到店铺包邮类别的 文字显示
	 * 创建店铺时  显示
	 * @author liushaomin
	 * 2017年6月22日
	 * @param postage_type
	 * @param mangou_money
	 * @return
	 */
	public static String getPostage_typeName(int postage_type, double mangou_money) {
		String postage_typeName = "";
		if(postage_type == 0){
			postage_typeName = "全场包邮";
		}else if(postage_type == 1){
			postage_typeName = "满购包邮";
		}else{
			postage_typeName = "另付邮费";
		}
		return postage_typeName;
	}
	
	/**
	 * 得到店铺包邮类别的 文字显示 
	 * 商品详情显示
	 * @author liushaomin
	 * 2017年6月23日
	 * @param phone
	 * @return
	 */
	public static String getShopPostage(String phone) {
		String sql = "select postage_type, mangou_money, postage_money from buyer_statistics where buyer_phone = '"+ phone +"'";
		BuyerStatisticsModel buyer = dao.findFirst(sql);
		String postage_typeName = "";
		if(buyer.getInt("postage_type") == 0){
			postage_typeName = "全场包邮";
		}else if(buyer.getInt("postage_type") == 1){
			postage_typeName = "满购¥" + buyer.getDouble("mangou_money") + "包邮";
		}else{
			postage_typeName = "另付邮费";
		}
		return postage_typeName;
	}
	
	/**
	 * 得到店铺包邮类别的文字显示   订单列表显示使用
	 * @author liushaomin
	 * 2017年7月12日
	 * @param seller_id 商家id
	 * @return
	 */
	public static String getShopPostage_order(String seller_id, double ordermoney) {
		String sql = "select postage_type, mangou_money, postage_money from buyer_statistics where buyer_id = "+ seller_id +"";
		BuyerStatisticsModel buyer = dao.findFirst(sql);
		String postage_typeName = "";
		if(buyer.getInt("postage_type") == 0){
			postage_typeName = "包邮";
		}else if(buyer.getInt("postage_type") == 1){
			if(ordermoney > buyer.getDouble("mangou_money")){
				postage_typeName = "包邮";
			}else{
				postage_typeName = "快递：¥" + buyer.getDouble("postage_money");
			}
		}else{
			postage_typeName = "快递：¥" + buyer.getDouble("postage_money");
		}
		return postage_typeName;
	}
	
}
